package easier.fast.app;

import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import easier.app.x.starter.ActivityTaskManager;

/**
 * 一个通用的app,可以管理全局的activity
 */
public class App extends Application {

    private final ActivityTaskManager mActivityTaskManager = new ActivityTaskManager();

    /**
     * @param context 退出app
     */
    public static void finishApp( @NonNull Context context ) {

        Context contextApplicationContext = context.getApplicationContext();
        if ( contextApplicationContext instanceof App ) {
            ((App) contextApplicationContext).mActivityTaskManager.finishAll();
        }
    }

    /**
     * 是否在前台
     *
     * @param context context
     *
     * @return true:在前台
     */
    public static boolean isForeground( @NonNull Context context ) {

        Context appContext = context.getApplicationContext();
        if ( appContext instanceof App ) {
            return ((App) appContext).mActivityTaskManager.getVisible() > 0;
        }

        return false;
    }

    @Override
    public void onCreate() {

        super.onCreate();

        mActivityTaskManager.register( this );
    }
}

